package com.gxl.butt.DAO.impl;

import com.gxl.butt.DAO.IKtopicDAO;
import com.gxl.butt.domain.FKTopic;
import com.gxl.butt.dto.bo.Topic;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by GUOXIAOLIN on 2018/7/13.
 */
@Repository
public class KtopicDAOImpl extends HibernateBaseDao<FKTopic> implements IKtopicDAO {

    @Override
    public boolean hasTopic(String topic) {

        String hql = "select topicname from " + FKTopic.class.getName() + " do where do.topicname = '" + topic + "'";
        List<FKTopic> topicList =  null;
        topicList = this.getListByHql(hql);
        if (topicList != null && topicList.size() > 0){
            return true;
        }
        return false;
    }

    @Override
    public Long saveTopic(FKTopic topic) {
        System.out.println("topic >>>>" + topic.toString());
        return (Long) this.save(topic);

    }

    @Override
    public List<FKTopic> getTopicListByUserId(Long userid) {

        String hql = "from " + FKTopic.class.getName() + " do  where  do.userid = " + userid;
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            return fkTopics;
        }

        return null;
    }

    @Override
    public FKTopic getTopicInfoByTopicId(Long tid) {

        String hql = "from " + FKTopic.class.getName() + " do  where  do.id = " + tid;
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            return fkTopics.get(0);
        }
        return null;

    }

    @Override
    public FKTopic getTopicInfoByTopicName(String topicname) {
        String hql = "from " + FKTopic.class.getName() + " do where do.topicname = " + topicname;
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            return fkTopics.get(0);
        }
        return null;

    }

    @Override
    public List<FKTopic> getTopicListByOpenid(String openid) {
//        System.out.println(openid+"-----=================================");
        String hql = "from " + FKTopic.class.getName() + " do where do.openid='" + openid+"' order by id desc";
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            return fkTopics;
        }

        return null;
    }

    @Override
    public List<FKTopic> getTopicByOpenIdAndTopicId(String topicid, String openid) {
        String hql = "from " + FKTopic.class.getName() + " do where do.openid='" + openid+"' and id='" + topicid +  "'";
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            return fkTopics;
        }
        return null;
    }

    @Override
    public void updateTopicNumbers(String topicname, String openid) {
        String hql = "from " + FKTopic.class.getName() + " do where do.openid='" + openid+"' and do.topicname = '" + topicname + "'";
        List<FKTopic> fkTopics = this.getListByHql(hql);
        if (fkTopics != null && fkTopics.size() >0 ){
            FKTopic fkTopic = fkTopics.get(0);
            fkTopic.setNumbers(fkTopic.getNumbers() + 1L);
        }
    }

    @Override
    public void updateTopic(FKTopic fkTopic) {
        this.update(fkTopic);

    }
}
